#!/bin/sh
. /lib/functions.sh

_setup_macaddr() {
	uci -q get "network.$1.macaddr" >/dev/null && return
	uci -q set "network.$1.macaddr=$2"
}

_setup_macvlan() {
	uci -q get "network.$1_dev.ifname" >/dev/null && return

	# do not create macvlan for vlan
	local _ifname
	_ifname=$(uci -q get "network.$1.device")
	case "$_ifname" in
	eth*.*) return ;;
	esac

	uci -q batch <<-EOF
	set network.$1_dev=device
	set network.$1_dev.name=$1
	set network.$1_dev.type=macvlan
	set network.$1_dev.ifname=$_ifname
	set network.$1_dev.mode='vepa'
	set network.$1_dev.txqueuelen=20
	set network.$1.device=$1
	set network.$1.type=macvlan
	set network.$1.masterintf=$_ifname
	EOF
	_macaddr=$(uci -q get "network.$1.macaddr")
	_setup_macaddr "$1_dev" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
}

#_setup_macvlan_update() {
#	uci -q get "network.$1_dev.device" >/dev/null || return
#
#	uci -q batch <<-EOF
#	set macvlan.$1=macvlan
#	set macvlan.$1.device=$_ifname
#	commit macvlan
#	EOF
#}

_setup_mptcp_handover_to_on() {
	if [ "$(uci -q get network.$1.multipath)" = "handover" ]; then
		uci -q set network.$1.multipath=on
	fi
	if [ "$(uci -q get openmptcprouter.$1.multipath)" = "handover" ]; then
		uci -q set openmptcprouter.$1.multipath=on
	fi
}

_setup_multipath_off() {
    uci -q get "network.$1.multipath" >/dev/null && return
    uci -q set "network.$1.multipath=off"
}

_setup_wan_interface() {
	uci -q batch <<-EOF
	set network.$1=interface
	set network.$1.device=$2
	set network.$1.proto=static
	set network.$1.ip4table=wan
	set network.$1.multipath=$3
	set network.$1.defaultroute=0
	set network.$1.addlatency=0
	set network.${1}_dev=device
	set network.${1}_dev.name=$2
	set network.${1}_dev.txqueuelen=20
	commit network
	add_list firewall.@zone[1].network=$1
	commit firewall
	EOF
	[ -n "$4" ] && uci -q set network.$1.type=$4
}

config_load network
#config_foreach _setup_macvlan_update interface
config_foreach _setup_mptcp_handover_to_on interface

if [ "$(uci -q show network.lan | grep multipath)" != "" ]; then
	exit 0
fi

lanif="eth0"
if [ "$(grep rockchip /etc/os-release)" != "" ]; then
	lanif="eth1"
elif [ -d /sys/class/net/lan0 -o -n "$(ip link | grep ' lan0')" ] && [ -d /sys/class/net/wan -o -n "$(ip link | grep ' wan@')" -o -n "$(ip link | grep ' wan:')" ]; then
	lanif="wan"
elif [ -d /sys/class/net/lan1 -o -n "$(ip link | grep ' lan1')" ] && [ -d /sys/class/net/wan -o -n "$(ip link | grep ' wan@')" -o -n "$(ip link | grep ' wan:')" ]; then
	lanif="wan"
elif [ -d /sys/class/net/lan ] || [ -n "$(ip link | grep ' lan')" ]; then
	lanif="lan"
elif [ "$(swconfig list 2>&1 | grep switch0)" != "" ] && [ "$(cat /etc/board.json | jsonfilter -q -e '@.model.platform' | tr -d '\n')" = "RUTX" ]; then
	lanif="eth1"
	uci -q batch <<-EOF
	set network.@switch_vlan[0]=switch_vlan
	set network.@switch_vlan[0].device='switch0'
	set network.@switch_vlan[0].vlan=1
	set network.@switch_vlan[0].ports='1t 2t 3t 4t 0t'
	set network.@switch_vlan[1]=switch_vlan
	set network.@switch_vlan[1].device='switch0'
	set network.@switch_vlan[1].vlan=2
	set network.@switch_vlan[1].ports='0 5'
	add network switch_vlan
	set network.@switch_vlan[2].device='switch0'
	set network.@switch_vlan[2].vlan=3
	set network.@switch_vlan[2].ports='0t 1'
	add network switch_vlan
	set network.@switch_vlan[3].device='switch0'
	set network.@switch_vlan[3].vlan=4
	set network.@switch_vlan[3].ports='0t 2'
	add network switch_vlan
	set network.@switch_vlan[4].device='switch0'
	set network.@switch_vlan[4].vlan=5
	set network.@switch_vlan[4].ports='0t 3'
	add network switch_vlan
	set network.@switch_vlan[5].device='switch0'
	set network.@switch_vlan[5].vlan=6
	set network.@switch_vlan[5].ports='0t 4'
	EOF
elif [ "$(swconfig list 2>&1 | grep switch0)" != "" ] && [ -d '/sys/class/net/eth1.5' ]; then
	lanif="eth1.5"
	uci -q batch <<-EOF
	set network.@switch_vlan[0]=switch_vlan
	set network.@switch_vlan[0].device='switch0'
	set network.@switch_vlan[0].vlan=1
	set network.@switch_vlan[0].vid=1
	set network.@switch_vlan[0].ports='3 5t'
	add network switch_vlan
	set network.@switch_vlan[1].device='switch0'
	set network.@switch_vlan[1].vlan=2
	set network.@switch_vlan[1].vid=2
	set network.@switch_vlan[1].ports='2 5t'
	add network switch_vlan
	set network.@switch_vlan[2].device='switch0'
	set network.@switch_vlan[2].vlan=3
	set network.@switch_vlan[2].vid=3
	set network.@switch_vlan[2].ports='1 5t'
	add network switch_vlan
	set network.@switch_vlan[3].device='switch0'
	set network.@switch_vlan[3].vlan=4
	set network.@switch_vlan[3].vid=4
	set network.@switch_vlan[3].ports='0 5t'
	add network switch_vlan
	set network.@switch_vlan[4].device='switch0'
	set network.@switch_vlan[4].vlan=5
	set network.@switch_vlan[4].vid=5
	set network.@switch_vlan[4].ports='4 6t'
	EOF
elif [ "$(swconfig list 2>&1 | grep switch0)" != "" ] && [ -d /sys/class/net/eth1 ] && [ "$(grep ipq806x /etc/os-release)" != "" ]; then
	lanif="eth0.2"
elif [ "$(swconfig list 2>&1 | grep switch0)" != "" ] && [ -d /sys/class/net/eth1 ]; then
	lanif="eth1"
elif [ ! -d /sys/class/net/eth1 ] && [ -d /sys/class/net/eth0 ]; then
	lanif="eth0"
fi
uci -q batch <<-EOF
delete network.lan.type
set network.lan=interface
set network.lan.proto=static
set network.lan.ipaddr=192.168.100.1
set network.lan.netmask=255.255.255.0
set network.lan.device=${lanif}
set network.lan.ifname=${lanif}
set network.lan.metric=2048
set network.lan.ipv6=0
set network.lan.delegate=0
set network.lan.addlatency=0
EOF

uci -q batch <<-EOF
delete network.none
delete network.wan
delete network.if6rd
reorder network.loopback=0
reorder network.globals=1
reorder network.lan=2
set network.globals.multipath=enable
EOF

# Set the ip rule for the lan with a pref of 100
uci -q show network.lan_rule >/dev/null || \
	uci -q batch <<-EOF
	set network.lan_rule=rule
	set network.lan_rule.lookup=lan
	set network.lan_rule.priority=100
	EOF

if [ "$(uci -q get network.vpn0.proto)" = "none" ]; then
	uci -q delete network.vpn0
fi

config_load network
config_foreach _setup_multipath_off interface

# Add the lan as a named routing table
if ! grep -s -q "lan" /etc/iproute2/rt_tables; then
	echo "50 lan" >> /etc/iproute2/rt_tables
fi
uci -q set network.lan.ip4table='lan'

#uci -q set "network.lan.ip6assign=64"

# Create WAN interfaces
if [ "$(uci -q show network.wan1 | grep multipath)" = "" ] && [ -z "$(uci -q get network.wan1.multipath)" ]; then
	if [ "$(grep ipq806x /etc/os-release)" != "" ]; then
		_setup_wan_interface wan1 eth1.1 master
		_setup_wan_interface wan2 eth1.2 on
		_setup_wan_interface wan3 eth1.3 on
		_setup_wan_interface wan4 eth1.4 on
	elif [ "$(grep rockchip /etc/os-release)" != "" ]; then
		_setup_wan_interface wan1 eth0 master macvlan
		_setup_wan_interface wan2 eth0 on macvlan
		_setup_macvlan wan1
		_setup_macvlan wan2
	elif [ "$(cat /etc/board.json | jsonfilter -q -e '@.model.platform' | tr -d '\n')" = "RUTX" ]; then
		_setup_wan_interface wan1 eth0.3 master
		_setup_wan_interface wan2 eth0.4 on
		_setup_wan_interface wan3 eth0.5 on
		_setup_wan_interface wan4 eth0.6 on
	elif [ "$(swconfig list 2>&1 | grep switch0)" != "" ]; then
		_setup_wan_interface wan1 eth0.1 master
		_setup_wan_interface wan2 eth0.2 on
		_setup_wan_interface wan3 eth0.3 on
		_setup_wan_interface wan4 eth0.4 on
	elif [ -d /sys/class/net/wan ] || [ -n "$(ip link | grep ' wan:')" ] || [ -n "$(ip link | grep ' wan@')" ]; then
		if [ -d /sys/class/net/lan0 -o -n "$(ip link | grep ' lan0')" ] && [ -d /sys/class/net/lan1 -o -n "$(ip link | grep ' lan1')" ]; then
			_setup_wan_interface wan1 lan0 master
			_setup_wan_interface wan2 lan1 on

			_macaddr=$(uci -q get "network.lan0.macaddr")
			_setup_macaddr "wan1" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
			_macaddr=$(uci -q get "network.lan1.macaddr")
			_setup_macaddr "wan2" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
			if [ -d /sys/class/net/lan2 ] || [ -n "$(ip link | grep ' lan2')" ]; then
				_setup_wan_interface wan3 lan2 on
				_macaddr=$(uci -q get "network.lan2.macaddr")
				_setup_macaddr "wan3" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
				if [ -d /sys/class/net/lan3 ] || [ -n "$(ip link | grep ' lan3')" ]; then
					_setup_wan_interface wan4 lan3 on
					_macaddr=$(uci -q get "network.lan3.macaddr")
					_setup_macaddr "wan4" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
				fi
			fi
		elif [ -d /sys/class/net/lan1 -o -n "$(ip link | grep ' lan1')" ] && [ -d /sys/class/net/lan2 -o -n "$(ip link | grep ' lan2')" ]; then
			_setup_wan_interface wan1 lan1 master
			_setup_wan_interface wan2 lan2 on

			_macaddr=$(uci -q get "network.lan1.macaddr")
			_setup_macaddr "wan1" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
			_macaddr=$(uci -q get "network.lan2.macaddr")
			_setup_macaddr "wan2" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
			if [ -d /sys/class/net/lan3 ] || [ -n "$(ip link | grep ' lan3')" ]; then
				_setup_wan_interface wan3 lan3 on
				_macaddr=$(uci -q get "network.lan3.macaddr")
				_setup_macaddr "wan3" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
				if [ -d /sys/class/net/lan4 ] || [ -n "$(ip link | grep ' lan4')" ]; then
					_setup_wan_interface wan4 lan4 on
					_macaddr=$(uci -q get "network.lan4.macaddr")
					_setup_macaddr "wan4" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
				fi
			fi
		else
			_setup_wan_interface wan1 wan master macvlan
			_setup_wan_interface wan2 wan on macvlan
			_setup_macvlan wan1
			_setup_macvlan wan2
		fi
	elif [ -d /sys/class/net/wan1 ] || [ -n "$(ip link | grep ' wan1')" ]; then
		if [ -d /sys/class/net/wan2 ] || [ -n "$(ip link | grep ' wan2')" ]; then
			_setup_wan_interface wan1 wan1 master
			_setup_wan_interface wan2 wan2 on

			_macaddr=$(uci -q get "network.wan1.macaddr")
			_setup_macaddr "wan1" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
			_macaddr=$(uci -q get "network.wan2.macaddr")
			_setup_macaddr "wan2" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"

			if [ -d /sys/class/net/wan3 ] || [ -n "$(ip link | grep ' wan3')" ]; then
				_setup_wan_interface wan3 wan3 on
				_macaddr=$(uci -q get "network.wan3.macaddr")
				_setup_macaddr "wan3" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
				if [ -d /sys/class/net/wan4 ] || [ -n "$(ip link | grep ' wan4')" ]; then
					_setup_wan_interface wan4 wan4 on
					_macaddr=$(uci -q get "network.wan4.macaddr")
					_setup_macaddr "wan4" "${_macaddr:-$(dd if=/dev/urandom bs=1024 count=1 2>/dev/null | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5:\6/' -e 's/^\(.\)[13579bdf]/\10/')}"
				fi
			fi
		else
			_setup_wan_interface wan1 wan1 master macvlan
			_setup_wan_interface wan2 wan1 on macvlan
			_setup_macvlan wan1
			_setup_macvlan wan2
		fi
	elif [ -d /sys/class/net/eth1 ] || [ -n "$(ip link | grep ' eth1:')" ]; then
		if [ -d /sys/class/net/eth2 ] || [ -n "$(ip link | grep ' eth2:')" ]; then
			_setup_wan_interface wan1 eth1 master
			_setup_wan_interface wan2 eth2 on
			if [ -d /sys/class/net/eth3 ] || [ -n "$(ip link | grep ' eth3:')" ]; then
				_setup_wan_interface wan3 eth3 on
			fi
			if [ -d /sys/class/net/eth4 ] || [ -n "$(ip link | grep ' eth4:')" ]; then
				_setup_wan_interface wan4 eth4 on
			fi
			if [ -d /sys/class/net/eth5 ] || [ -n "$(ip link | grep ' eth5:')" ]; then
				_setup_wan_interface wan5 eth5 on
			fi
			if [ -d /sys/class/net/eth6 ] || [ -n "$(ip link | grep ' eth6:')" ]; then
				_setup_wan_interface wan6 eth6 on
			fi
			if [ -d /sys/class/net/eth7 ] || [ -n "$(ip link | grep ' eth7:')" ]; then
				_setup_wan_interface wan7 eth7 on
			fi
			if [ -d /sys/class/net/eth8 ] || [ -n "$(ip link | grep ' eth8:')" ]; then
				_setup_wan_interface wan8 eth8 on
			fi
		else
			_setup_wan_interface wan1 eth1 master macvlan
			_setup_wan_interface wan2 eth1 on macvlan
			_setup_macvlan wan1
			_setup_macvlan wan2
		fi
	elif [ -d /sys/class/net/eth0.1 ] && [ -d /sys/class/net/eth0.2 ]; then
		_setup_wan_interface wan1 eth0.1 master
		_setup_wan_interface wan2 eth0.2 on
	else
		_setup_wan_interface wan1 eth0 master macvlan
		_setup_wan_interface wan2 eth0 on macvlan
		_setup_macvlan wan1
		_setup_macvlan wan2
	fi
	#uci -q batch <<-EOF
	#add network route6
	#set network.@route6[-1].interface='lan'
	#set network.@route6[-1].target='::/0'
	#EOF
fi

# Replace omrip to oip in config for old config
sed -i 's/omrip/oip/g' /etc/config/*

# Fix config from ifname to device for loopback
uci -q delete network.loopback.ifname
uci -q set network.loopback.device='lo'

local board=$(board_name)
if [ "$board" = "teltonika,rutx" ] && [ -f /sbin/mnf_info ]; then
	# Same part for RUTX12 and RUTX11, maybe other RUTX ?
	uci -q batch <<-EOF
	set network.modem1=interface
	set network.modem1.proto='modemmanager'
	set network.modem1.apn=''
	set network.modem1.auth='none'
	set network.modem1.iptype='ipv4v6'
	set network.modem1.addlatency='0'
	set network.modem1.force_link='1'
	set network.modem1.device='/sys/devices/platform/soc/60f8800.usb2/6000000.dwc3/xhci-hcd.1.auto/usb3/3-1'
	set network.modem1.peerdns='0'
	set network.modem1.multipath='on'
	set network.modem1.defaultroute='0'
	commit network
	add_list firewall.@zone[1].network='modem1'
	commit firewall
	EOF
	if [ "$(mnf_info -n)" = "RUTX1200XXXX" ]; then
		uci -q batch <<-EOF
		set network.modem2=interface
		set network.modem2.proto='modemmanager'
		set network.modem2.apn=''
		set network.modem2.auth='none'
		set network.modem2.iptype='ipv4v6'
		set network.modem2.addlatency='0'
		set network.modem2.force_link='1'
		set network.modem2.device='/sys/devices/platform/soc/8af8800.usb3/8a00000.dwc3/xhci-hcd.0.auto/usb1/1-1/1-1.2'
		set network.modem2.peerdns='0'
		set network.modem2.multipath='on'
		set network.modem2.defaultroute='0'
		set network.modem2.ip4table=wan
		commit network
		add_list firewall.@zone[1].network='modem2'
		commit firewall
		EOF
	fi
	uci -q batch <<-EOF
	set network.wifi24=interface
	set network.wifi24.proto='none'
	set network.wifi5=interface
	set network.wifi5.proto='none'
	commit network
	set wireless.radio0.cell_density='0'
	set wireless.default_radio0.network='wifi24'
	set wireless.radio1.cell_density='0'
	set wireless.default_radio1.network='wifi5'
	commit wireless
	add ucitrack led
	set ucitrack.@led[-1].init=led
	del_list ucitrack.@firewall[0].affects=led
	add_list ucitrack.@firewall[0].affects=led
	commit ucitrack
	EOF

fi


uci -q commit macvlan
uci -q commit network
rm -f /tmp/luci-indexcache
exit 0
